<template>

    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades">设备台账</i>
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-select v-model="query.equipName" placeholder="设备名称" class="handle-select mr10">
                    <el-option
                            v-for="item in equipNameList"
                            :key="item"
                            :label="item"
                            :value="item">
                    </el-option>
                </el-select>
                <el-select v-model="query.productLine" placeholder="所在产线" class="handle-select mr10">
                    <el-option
                            v-for="item in productLine"
                            :key="item.id"
                            :label="item.lineName"
                            :value="item.id">
                    </el-option>
                </el-select>
                <el-input v-model="query.equipNum" placeholder="输入编号查询" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <!--        增加记录        -->
                <el-button type="primary" icon="el-icon-zoom-in" @click="handleAdd">添加</el-button>
                <el-button icon="el-icon-download" @click="exportExcel">导出</el-button>
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>

            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    id="out-table"
                    ref="multipleTable"

                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>

                <el-table-column type="index" width="55" align="center" label="序号"></el-table-column>
                <el-table-column prop="equipNum" align="center" label="设备编号" sortable width="110"></el-table-column>
                <el-table-column prop="equipName" align="center" label="设备名称" sortable width="110"></el-table-column>
                <el-table-column prop="standards" align="center" label="规格型号" sortable width="110"></el-table-column>
                <el-table-column prop="manufacturer"  align="center" label="生产厂商" sortable width="110"></el-table-column>
                <el-table-column prop="productLine.lineName" align="center" label="所在产线" sortable width="110"></el-table-column>
                <el-table-column prop="status" align="center" :formatter="statusFormat" label="设备状况"></el-table-column>
                <el-table-column prop="operator" align="center" label="操作人员" sortable width="110"></el-table-column>
                <el-table-column prop="buyTime"  align="center" label="购置时间" sortable width="110"></el-table-column>
                <el-table-column prop="assets" align="center" label="资产原值" sortable width="110"></el-table-column>
                <el-table-column prop="equipType" align="center" label="设备类别" sortable width="110"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>

            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                        align="center"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="95px">
                <!--<el-form-item label="序号"><el-input v-model="form.id"></el-input></el-form-item>-->
                <el-form-item label="设备编号"><el-input v-model="form.equipNum"></el-input></el-form-item>
                <el-form-item label="设备名称"><el-input v-model="form.equipName"></el-input></el-form-item>
                <el-form-item label="规格型号"><el-input v-model="form.standards"></el-input></el-form-item>
                <el-form-item label="生产厂商"><el-input v-model="form.manufacturer"></el-input></el-form-item>
                <el-form-item label="所在产线">
                    <el-select v-model="form.productLine" value-key="id" class="handle-select mr10">
                        <el-option
                                v-for="item in productLine"
                                :key="item.id"
                                :label="item.lineName"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备状况" >
                    <el-select v-model="form.status" class="handle-select mr10">
                        <el-option key="1" label="闲置中" :value='0'></el-option>
                        <el-option key="2" label="运行中" :value='1'></el-option>
                        <el-option key="3" label="故障中" :value='-1'></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="操作人员"><el-input v-model="form.operator"></el-input></el-form-item>
                <el-form-item label="购置时间" >
                    <el-date-picker
                            v-model="form.buyTime"
                            type="date"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="资产原值"><el-input v-model="form.assets"></el-input></el-form-item>
                <el-form-item label="设备类别"><el-input v-model="form.equipType"></el-input></el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="form" :rules="rules" label-width="95px">
                <el-form-item label="设备编号" prop="equipNum"><el-input v-model="form.equipNum"></el-input></el-form-item>
                <el-form-item label="设备名称" prop="equipName"><el-input v-model="form.equipName"></el-input></el-form-item>
                <el-form-item label="规格型号"><el-input v-model="form.standards"></el-input></el-form-item>
                <el-form-item label="生产厂商"><el-input v-model="form.manufacturer"></el-input></el-form-item>
                <el-form-item label="所在产线">
                    <el-select v-model="form.productLine" value-key="id" class="handle-select mr10">
                        <el-option
                                v-for="item in productLine"
                                :key="item.id"
                                :label="item.lineName"
                                :value="item">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="设备状况" >
                    <el-select v-model="form.status" class="handle-select mr10">
                        <el-option key="1" label="闲置中" :value='0'></el-option>
                        <el-option key="2" label="运行中" :value='1'></el-option>
                        <el-option key="3" label="故障中" :value='-1'></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="操作人员"><el-input v-model="form.operator"></el-input></el-form-item>
                <el-form-item label="购置时间">
                    <el-date-picker
                            v-model="form.buyTime"
                            type="date"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="资产原值"><el-input v-model="form.assets"></el-input></el-form-item>
                <el-form-item label="设备类别"><el-input v-model="form.equipType"></el-input></el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    import FileSaver from 'file-saver'
    import XLSX from 'xlsx'
    export default {
        name: "equipReport",
        data() {
            return {
                query: {
                    equipName:'',
                    productLine:'',
                    equipNum: '',
                    pageIndex: 1,
                    pageSize: 30
                },
                productLine:[],
                equipNameList:[],
                tableData: [],
                multipleSelection: [],
                delList: [],
                addVisible: false,
                editVisible: false,
                pageTotal: 0,
                form: {
                    productLine:"",
                    status:0
                },
                idx: -1,
                id: -1,
                rules: {
                    equipNum: [{ required: true, message: '请输入设备编号', trigger: 'blur' }],
                    equipName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
                },

            };

        },
        created() {
            this.getData();
            this.getProductLine();
            this.getEquipName();

        },
        methods: {

            //批量删除
            delAllSelection(){
                const length = this.multipleSelection.length;
                if (length===0){
                    this.$message.warning("请选择要删除的设备");
                }else {

                    this.$confirm('确定要删除吗？', '提示', {
                        type: 'warning'
                    })
                        .then(() => {
                            let flag = 0;
                            for (let i = 0; i < length; i++) {
                                this.$axios.get('/api/equip/delete?id=' + this.multipleSelection[i].id).then(res => {

                                    if (res.data.code !== "200") {
                                        flag = 1;
                                    }
                                    this.getData();
                                })

                            }
                            this.multipleSelection = [];
                            if (flag === 0) {
                                this.$message.success("删除成功");
                            } else {
                                this.$message.error("删除失败");
                            }

                        })
                        .catch(() => {
                        });
                }
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            //获取设备名称下拉表
            getEquipName(){
                this.$axios.get('/api/equip/getEquipName').then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message)
                    }else {
                        this.equipNameList = res.data.data;
                    }

                })
            },
            //tableData 翻译status
            statusFormat(row, column){

                if (row.status === 0) {
                    return '闲置中'
                } else if (row.status === 1) {
                    return '运行中'
                } else if (row.status === -1) {
                    return '故障中'
                }
            },
            //查询产线下拉列表
            getProductLine(){
                this.$axios.get('/api/productLine/selectAll').then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message);
                    }else {
                        this.productLine = res.data.data;
                        this.form.productLine=res.data.data[0];
                    }

                })
            },

            getData() {
                this.$axios.get('/api/equip/selectAll').then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message);
                    }else {
                        this.pageSplit(res.data.data);
                    }

                })
            },
            // 编辑操作
            handleEdit(index, row) {

                this.idx = index;
                this.form = row;

                this.editVisible = true;
            },
            // 删除操作
            handleDelete(row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                    .then(() => {

                        this.$axios.get('/api/equip/delete?id='+row.id).then(res=>{
                            if(res.data.code==="200"){
                                this.$message.success("删除成功");
                                this.getData();
                            }else {
                                this.$message.error(res.data.message);
                            }

                        })
                    })
            },
            // 保存编辑
            saveEdit() {
                console.log(this.form);

                this.editVisible = false;
                this.$axios.post('/api/equip/edit',this.form).then(res=>{
                    if(res.data.code=="200"){
                        this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                        this.getData();
                    }else {
                        this.$message.error(`修改第 ${this.idx + 1} 行失败`);
                    }

                });

            },
            // 保存添加
            saveAdd() {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        console.log(this.form);

                        this.addVisible = false;
                        this.$axios.post('/api/equip/add',this.form).then(res=>{
                            if(res.data.code==='200'){
                                this.$message.success(res.data.message);
                                this.getData();
                            }else {
                                this.$message.error(res.data.message);
                            }
                        });
                    }
                    else {
                        this.$message.error('请检查输入');
                        console.log('error submit!!');
                        return false;
                    }
                })
            },
            // 触发搜索按钮
            handleSearch() {
                this.$axios.get('/api/equip/search?name='+this.query.equipName+'&line='+this.query.productLine+'&equipNum='+this.query.equipNum).then(res =>{
                    if (res.data.code!=='200') {
                        this.$message.error(res.data.message);
                    }else {
                        this.pageSplit(res.data.data);
                    }
                });
                // this.toString();

            },
            pageSplit(data){
                this.pageTotal=data.length;
                let start= (this.query.pageIndex-1)*this.query.pageSize;
                let end = this.query.pageIndex*this.query.pageSize<this.pageTotal?this.query.pageIndex*this.query.pageSize:this.pageTotal;
                this.tableData=[];
                for (let i=start;i<end;i++){
                    this.tableData[i-start]=data[i];
                }

            },

            //添加操作
            handleAdd() {
                this.addVisible = true;
            },
            exportExcel () {
                /* out-table关联导出的dom节点  */
                var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
                /* get binary string as output */
                var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
                try {
                    FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), '安卓PAD.xlsx')
                } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
                return wbout
            },
            // 分页导航
            handlePageChange(val) {

                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }

        }
    }
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>